package path;

import geom.PolarVector;
import geom.Vector;
import geom.VectorSpace;

import java.util.ArrayList;
import java.util.Iterator;

import processing.core.PApplet;
import processing.core.PMatrix;
import shapes.Vertex;

public class DrawPolygonPath implements PathDrawer{
	ArrayList center;
	ArrayList left;
	ArrayList right;

	float width;
	
	public DrawPolygonPath(float width) {
		center = new ArrayList();
		left = new ArrayList();
		right = new ArrayList();
		this.width = width;
	}

	public void keysAdded(Path p, int amount){
		if(p.getSize() <= 2) return;
		int backStep = 0;
		if(left.size() >= 2 && right.size() >= 2){
			left.remove(left.size()-1);
			right.remove(right.size()-1);
			center.remove(center.size()-1);
			backStep = 1;
		}
		for(int i = p.getSize() - amount - backStep; i < p.getSize(); i++){
			Vector[] outline = getOutline(p, i, width);
			Vector c = p.getKeyFrameByIndex(i).getPosition();
			left.add(outline[0]);
			center.add(new Vertex(c, c.normalize()));
			right.add(outline[1]);
		}
	}

	public void keyRemoved(Path p, int i) {
		if(left.size() <= i) return;
		//System.out.println("remove: " + i + " size " + left.size());
		left.remove(i);
		right.remove(i);
		center.remove(i);
	}
	
	public void keyAdded(Path p) {
		keysAdded(p, 1);
	}
	
	public void drawPath(Path path, PApplet pa) {
		/*
		for(int i = 0; i < path.getSize(); i++){
			Vector v1 = null, v2, v3 = null;
			
			VectorSpace tnbSpace = null;
			v2 = path.getKeyFrameByIndex(i).getPosition();
			if(i > 0 && i < path.getSize() - 1){ 
				v1 = path.getKeyFrameByIndex(i-1).getPosition();
				v3 = path.getKeyFrameByIndex(i+1).getPosition();
				
			}
			
			else if(i == 0 && i < path.getSize() -1){
				v1 = path.getKeyFrameByIndex(i).getPosition();
				v3 = path.getKeyFrameByIndex(i+1).getPosition();
			}
			else if(i >= path.getSize() - 1 && i > 0){
				v1 = path.getKeyFrameByIndex(i-1).getPosition();
				v3 = path.getKeyFrameByIndex(i).getPosition();
			}
			
			
			if(v1 != null || v3 != null){
				tnbSpace = VectorSpace.newVectorSpaceFromCurveAndCenter(v1, v2, v3, new Vector());
				Vector c = tnbSpace.getOrthoCoord(new Vector());
				Vector x = tnbSpace.getOrthoCoord(new Vector(20f, 0, 0));
				Vector y = tnbSpace.getOrthoCoord(new Vector(0, 20f, 0));
				Vector z = tnbSpace.getOrthoCoord(new Vector(0, 0, 20f));
				//System.out.println(x.toString() + y.toString() + z.toString());
				pa.stroke(255, 0, 0);
				pa.line(c.x, c.y, c.z, x.x, x.y, x.z);
				pa.stroke(0, 255, 0);
				pa.line(c.x, c.y, c.z, y.x, y.y, y.z);
				pa.stroke(0, 0, 255);
				pa.line(c.x, c.y, c.z, z.x, z.y, z.z);
			}
			
		}
		*/
		
		pa.stroke(255);
		pa.fill(15, 20, 220, 180);
		
		for(int i = 1; i < left.size(); i++){
			pa.beginShape(PApplet.QUADS);
			getLeftVertex(i-1).draw(pa);
			getCenterVertex(i-1).draw(pa);
			getCenterVertex(i).draw(pa);
			getLeftVertex(i).draw(pa);
			pa.endShape();
			
			pa.beginShape(PApplet.QUADS);
			getCenterVertex(i-1).draw(pa);
			getRightVertex(i-1).draw(pa);
			getRightVertex(i).draw(pa);
			getCenterVertex(i).draw(pa);
			pa.endShape();
		}
		
	}
	
	private void scaleWidth(int i, float scale){
		Vector vLeft, vCenter, vRight;
		vLeft = (Vector)left.get(i);
		vCenter = (Vector)center.get(i);
		vRight = (Vector)right.get(i);
		vLeft = vLeft.sub(vCenter).mult(scale).add(vCenter);
		vRight = vRight.sub(vCenter).mult(scale).add(vCenter);
	}
	
	private Vertex[] getOutline(Path p, int i, float width){
		Vector v1 = null, v2, v3 = null;
		
		VectorSpace tnbSpace = null;
		
		v2 = p.getKeyFrameByIndex(i).getPosition();
		if(i > 0 && i < p.getSize() - 1){ 
			v1 = p.getKeyFrameByIndex(i-1).getPosition();
			v3 = p.getKeyFrameByIndex(i+1).getPosition();
			
		}
		
		else if(i == 0 && i < p.getSize() -1){
			v1 = p.getKeyFrameByIndex(i).getPosition();
			v3 = p.getKeyFrameByIndex(i+1).getPosition();
		}
		else if(i >= p.getSize() - 1 && i > 0){
			v1 = p.getKeyFrameByIndex(i-1).getPosition();
			v3 = p.getKeyFrameByIndex(i).getPosition();
		}
		
		else return new Vertex[]{
			new Vertex(new Vector(v2)),
			new Vertex(new Vector(v2))
		};
		
		tnbSpace = VectorSpace.newVectorSpaceFromCurveAndCenter(v1, v2, v3, new Vector());
		
		return new Vertex[]{
			new Vertex(tnbSpace.getOrthoCoord(new Vector(0, width*2f, 0))),
			new Vertex(tnbSpace.getOrthoCoord(new Vector(0,-width*2f, 0)))
		};
	}

	public Vertex getLeftVertex(int i){
		return (Vertex)left.get(i);
	}
	
	public Vertex getRightVertex(int i){
		return (Vertex)right.get(i);
	}
	
	public Vertex getCenterVertex(int i){
		return (Vertex)center.get(i);
	}

}
